﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.Security;
namespace EnterpriseFund.BLL
{
    /// <summary>
    /// 业务逻辑类PrvUser 的摘要说明。
    /// </summary>
    public class PrvUser
    {
        private readonly DAL.PrvUser dal = new DAL.PrvUser();
        public PrvUser()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FLogID, string FEntityNo)
        {
            return dal.Exists(FLogID, FEntityNo);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Model.PrvUser model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Model.PrvUser model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string FLogID, string FEntityNo)
        {

            dal.Delete(FLogID, FEntityNo);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.PrvUser GetModel(string FLogID, string FEntityNo)
        {

            return dal.GetModel(FLogID, FEntityNo);
        }

    

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.PrvUser> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.PrvUser> DataTableToList(DataTable dt)
        {
            List<Model.PrvUser> modelList = new List<Model.PrvUser>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.PrvUser model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.PrvUser();
                    model.FLogID = dt.Rows[n]["FLogID"].ToString();
                    model.FEntityNo = dt.Rows[n]["FEntityNo"].ToString();
                    model.FUserName = dt.Rows[n]["FUserName"].ToString();
                    model.Fpassword = dt.Rows[n]["Fpassword"].ToString();
                    if (dt.Rows[n]["FLastLoginTime"].ToString() != "")
                    {
                        model.FLastLoginTime = DateTime.Parse(dt.Rows[n]["FLastLoginTime"].ToString());
                    }
                    model.FLastLoginIPAddr = dt.Rows[n]["FLastLoginIPAddr"].ToString();
                    if (dt.Rows[n]["FLoginCnt"].ToString() != "")
                    {
                        model.FLoginCnt = int.Parse(dt.Rows[n]["FLoginCnt"].ToString());
                    }
                    if (dt.Rows[n]["FProhibitLogin"].ToString() != "")
                    {
                        model.FProhibitLogin = int.Parse(dt.Rows[n]["FProhibitLogin"].ToString());
                    }
                    if (dt.Rows[n]["FForeverAvailable"].ToString() != "")
                    {
                        model.FForeverAvailable = int.Parse(dt.Rows[n]["FForeverAvailable"].ToString());
                    }
                    if (dt.Rows[n]["FAvailableDate"].ToString() != "")
                    {
                        model.FAvailableDate = DateTime.Parse(dt.Rows[n]["FAvailableDate"].ToString());
                    }
                    model.FEmplNo = dt.Rows[n]["FEmplNo"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  成员方法

        #region 自己增加的方法

        /// <summary>
        /// 根据用户登录名和密码,公司编号判断用户是否存在
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ExistsByNameAndPassword(string loginid, string password, string entityNo)
        {
            string newPassword = getMd5Password(loginid, password, entityNo);
            return dal.ExistsByNameAndPassword(loginid, newPassword, entityNo);
        }

        /// <summary>
        /// 根据公司编号,用户登录名跟密码获取MD5密码
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public string getMd5Password(string loginid, string password, string entityNo)
        {
            string newPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(entityNo + loginid + password, "MD5");
            return newPassword;
        }

        /// <summary>
        /// 得到一个对象实体,根据用户登陆号、密码、单位编码
        /// </summary>
        /// <param name="logid"></param>
        /// <param name="password"></param>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public Model.PrvUser GetModelByNameAndPassword(string loginid, string password, string entityNo)
        {
            string newPassword = getMd5Password(loginid, password, entityNo);
            return dal.GetModelByNameAndPassword(loginid, newPassword, entityNo);
        }


        /// <summary>
        /// 取出相应的用户
        /// </summary>
        /// <param name="GroupNo">组名</param>
        /// <param name="LeftOrRight">输入left或right,确定是左边listbox还是右边listbox</param>
        /// <param name="entityNo">公司编号</param>
        /// <returns></returns>
        public DataSet GetListByGroup(string GroupNo, string LeftOrRight, string entityNo)
        {
            return dal.GetListByGroup(GroupNo, LeftOrRight, entityNo);
        }


        /// <summary>
        /// 获得数据列表,根据公司编号
        /// </summary>
        public DataSet GetListByEntityNo(string entityNo)
        {
            return dal.GetListByEntityNo(entityNo);
        }

        /// <summary>
        /// 从视图中获取数据列表，那部门和人员排序,部分字段 FLogID FUserName
        /// </summary>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public DataSet GetListByEntityNoOnView(string entityNo)
        {
            return dal.GetListByEntityNoOnView(entityNo);
        }

                /// <summary>
        /// 获取数据和部门编号 FLogID,FUserName,FDeptNo
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>用户信息列表</returns>
        public List<Model.PrvUser> GetListDept(string entityNo)
        {
            List<Model.PrvUser> list = new List<Model.PrvUser>();
            DataSet ds = dal.GetListDept(entityNo);
            Model.PrvUser model;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                model = new Model.PrvUser();
                model.FLogID = row["FLogID"].ToString();
                model.FUserName = row["FUserName"].ToString();
                model.FDeptNo = row["FDeptNo"].ToString();
                list.Add(model);
            }

            return list;
        }

  

        /// <summary>
        /// 根据公司编号和部门编号获取
        /// </summary>
        /// <param name="FEntityNo"></param>
        /// <param name="FEmplNo"></param>
        /// <returns></returns>
        public List<Model.PrvUser> GetModelListByEmplNo(string FEntityNo, string FDeptNo)
        {
            DataSet ds = dal.GetModelListByEmplNo(FEntityNo, FDeptNo);
            return DataTableToList(ds.Tables[0]);
        }

            /// <summary>
        /// 根据公司编号和部门编号获取
        /// </summary>
        /// <param name="FEntityNo"></param>
        /// <param name="FEmplNo"></param>
        /// <returns></returns>
        public List<Model.PrvUser> GetModelListInHREmpl(string FEntityNo, string FDeptNo)
        {
            DataSet ds = dal.GetModelListInHREmpl(FEntityNo, FDeptNo);
            return DataTableToList2(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.PrvUser> DataTableToList2(DataTable dt)
        {
            List<Model.PrvUser> modelList = new List<Model.PrvUser>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.PrvUser model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.PrvUser();
                    model.FUserName = dt.Rows[n]["FEmpName"].ToString();
                    model.FEmplNo = dt.Rows[n]["FEmplNo"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 根据用户登录号和公司编号获取员工ID
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="strLogId"></param>
        /// <returns></returns>
        public string getEmplNo(string strEntityNo, string strLogId)
        {
            return dal.getEmplNo(strEntityNo, strLogId);
        }


        /// <summary>
        /// 根据用户登录号和公司编号获取一个员工ID和员工名称
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="strLogId"></param>
        /// <returns></returns>
        public List<string> getEmplNoAndUserName(string strEntityNo, string strLogId)
        {
            return dal.getEmplNoAndUserName(strEntityNo, strLogId);
        }


        /// <summary>
        /// 获取员工ID和部门ID
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="strLogId"></param>
        /// <returns></returns>
        public List<string> getEmplNoAndDeptNo(string strEntityNo, string strLogId)
        {
            return dal.getEmplNoAndDeptNo(strEntityNo, strLogId);
        }

                       /// <summary>
        /// 获取员工ID和部门ID
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="strLogId"></param>
        /// <returns></returns>
        public List<string> getEmpl(string strEntityNo, string strLogId)
        {
            return dal.getEmpl(strEntityNo, strLogId);
        }

        /// <summary>
        /// 根据公司编号和登录ID获取员工编号和部门编号
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="strLogId"></param>
        /// <returns></returns>
        public List<string> getEmpNoAndDeptNo(string strEntityNo, string strLogId)
        {
            return dal.getEmpNoAndDeptNo(strEntityNo, strLogId);
        }

        #endregion

        /// <summary>
        /// 取消门户统一登陆绑定
        /// </summary>
        /// <param name="FLogID"></param>
        /// <param name="FEntityNo"></param>
        public void cancelUserUnifyLogin(string FLogID, string FEntityNo)
        {
            dal.cancelUserUnifyLogin(FLogID, FEntityNo);
        }
        public string GetThemePrvUser(string strEntityNo, string strLogId)
        {

            return dal.GetThemePrvUser(strEntityNo, strLogId);
        }
        public bool UpdateThemePrvUser(string strEntityNo, string strLogId, string Theme)
        {
            return dal.UpdateThemePrvUser(strEntityNo, strLogId, Theme);
        }
                /// <summary>
        /// 获取正级编号
        /// </summary>
        /// <param name="deptNo"></param>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public string GetPropEmpNo(string deptNo, string entityNo)
        {
            return dal.GetPropEmpNo(deptNo, entityNo);
        }
        
        /// <summary>
        /// 获取正级手机号
        /// </summary>
        /// <param name="deptNo"></param>   
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public string GetMobile(string emplNo, string entityNo)
        {
            return dal.GetMobile(emplNo, entityNo);
        }
    }
}

